package decodingSimulaiton;

import java.util.Random;

public class DecodingHelper {
	int k ;
	int n ;
	public DecodingHelper(int k, int n) {
		super();
		this.k = k;
		this.n = n;
	}
	public int[] serverAvailability() {
		int[] servers = new int[n];
		Random random = new Random();
		for (int i = 0; i < n; i++) {
			if (random.nextDouble() < 0.8) {
				servers[i] = 1;
			} else {
				servers[i] = 0;
			}
//			System.out.print(" " + servers[i]);
		}

		return servers;

	}
	public int[][] matrixTranspose(int[][] generator) {
		int[][] transpose;
		int rows = k;
		int cols = n;
		transpose = new int[cols][rows];

		for (int h = 0; h < rows; h++) {
			for (int i = 0; i < cols; i++) {
				transpose[i][h] = generator[h][i];
			}
		}

		return transpose;
	}
	
	 public int[][] identity(int k) {
	        int[][] I = new int[k][k];
	        for (int i = 0; i < k; i++)
	            I[i][i] = 1;
	        return I;
	    }
	 
	 public int gaussianElimination(int[][] G, int[] sourceIndex) {
	        int result =0;
			int[][] newGenerator = new int[k][k];
			int l = 0;
			for (int i : sourceIndex) {
				for (int j = 0; j < k; j++) {
					newGenerator[l][j] = G[i][j];
				}
				l++;
			}
			int n = k;
			int N = k;
			for (int p = 0; p < n; p++) {
				int max = p;
				for (int i = p + 1; i < N; i++) {
					if (Math.abs(newGenerator[i][p]) > Math
							.abs(newGenerator[max][p])) {
						max = i;
					}
				}
				int[] temp = newGenerator[p];
				newGenerator[p] = newGenerator[max];
				newGenerator[max] = temp;
				if (Math.abs(newGenerator[p][p]) != 1) {
					throw new RuntimeException(
							"Matrix is singular or nearly singular");
				}

				for (int i = p + 1; i < N; i++) {
					int alpha = Math.abs(-(newGenerator[i][p] / newGenerator[p][p]));
				
					for (int j = p; j < n; j++) {
						newGenerator[i][j] = newGenerator[i][j]
								^ (alpha & newGenerator[p][j]);
					}
				}
				for (int i = 0; i <= p - 1; i++) {
					int alpha =Math.abs( -(newGenerator[i][p] / newGenerator[p][p]));
					
					for (int j = p; j < n; j++) {
						newGenerator[i][j] = newGenerator[i][j]
								^ (alpha & newGenerator[p][j]);
					}
				}
			}
			result++;
			return result;
		}

}
